package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaUtil;
import com.hemaapp.yjnh.BaseAdapter;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.ShowLargeImageActivity;
import com.hemaapp.yjnh.listener.OnItemClickListener;
import com.hemaapp.yjnh.bean.Img;
import com.hemaapp.yjnh.bean.ImgItemsBean;
import com.hemaapp.yjnh.bean.Reply;
import com.hemaapp.yjnh.bean.Reply.ReplyChild;
import com.hemaapp.yjnh.utils.ImageUtils;
import com.hemaapp.yjnh.view.ImageGridView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**实物和预约类商品详情中的口碑评论
 * 用户口碑
 * @author ZhangGuiyou
 */
public class ReplyAdapter extends BaseAdapter {

	private Context mContext;
	private ArrayList<Reply> replies;
	private OnItemClickListener itemClickListener;

	public ReplyAdapter(Context mContext, ArrayList<Reply> replies) {
		super(mContext);
		this.mContext = mContext;
		this.replies = replies;
	}

	public void setItemClickListener(OnItemClickListener itemClickListener) {
		this.itemClickListener = itemClickListener;
	}

	@Override
	public int getCount() {
		int size = replies == null ? 0 : replies.size();
		return size == 0 ? 1 : size;
	}
	
	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}
	
	@Override
    public boolean isEmpty() {
        int size = replies == null? 0:replies.size();
        return size == 0;
    }

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (isEmpty()) 
			return getEmptyView(parent);
		ViewHolder holder;
		if (convertView == null || convertView.getTag(R.id.TAG_REPLY) == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.item_production_reply, parent, false);
			holder = new ViewHolder(convertView);
			convertView.setTag(R.id.TAG_REPLY, holder);
		}else
			holder = (ViewHolder) convertView.getTag(R.id.TAG_REPLY);
		holder.setDatas(position);
		return convertView;
	}
	class ViewHolder {
		@Bind(R.id.iv_avatar)
		ImageView ivAvatar;
		@Bind(R.id.tv_name)
		TextView tvName;
		@Bind(R.id.tv_date)
		TextView tvDate;
		@Bind(R.id.iv_reply)
		ImageView ivReply;
		@Bind(R.id.tv_reply)
		TextView tvReply;
		@Bind(R.id.image_grid)
		ImageGridView imageGrid;
		@Bind(R.id.tv_merchant_date)
		TextView tvMerchantDate;
		@Bind(R.id.tv_merchant_reply)
		TextView tvMerchantReply;
		@Bind(R.id.layout_merchant_reply)
		LinearLayout layoutMerchantReply;
		@Bind(R.id.line_merchant_reply)
		View lineMerchantReply;
		int position = -1;
		AddImageAdapter imageAdapter;
		ArrayList<ImgItemsBean> replyImgs = new ArrayList<>();

		ViewHolder(View view) {
			ButterKnife.bind(this, view);
			ivReply.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (itemClickListener != null && position != -1)
						itemClickListener.onClickListener(position);
				}
			});
		}

		private void setDatas(int position) {
			this.position = position;
			Reply reply = replies.get(position);
			ImageUtils.loadAvatar(mContext, reply.getAvatar(), ivAvatar);
			tvName.setText(reply.getNickname());
			tvDate.setText(HemaUtil.transTimeChat(reply.getRegdate()));
			//显示帖子内容，并将其中emoji表情进行转换
			HemaUtil.SetMessageTextView(mContext, tvReply, reply.getContent());
			replyImgs.clear();
			replyImgs.addAll(reply.getItems());
			refreshImages();
			//显示商家回复
			String sellerflag = reply.getSellerflag();// 商家回复标志	0否；1是
			if (sellerflag.equals("1")) {
				layoutMerchantReply.setVisibility(View.VISIBLE);
				ReplyChild child = reply.getChilds().get(0);
				tvMerchantDate.setText(HemaUtil.transTimeChat(child.getRegdate()));
				tvMerchantReply.setText(child.getContent());
			}else {
                layoutMerchantReply.setVisibility(View.GONE);
            }
		}

		private void refreshImages() {
			if (imageAdapter == null) {
				imageAdapter = new AddImageAdapter(mContext, replyImgs);
				imageGrid.setAdapter(imageAdapter);
				imageAdapter.setShowDelete(false);
				imageAdapter.setShowAdd(false);
				imageAdapter.setImageClickListener(new AddImageAdapter.OnImageClickListener() {
					@Override
					public void onImageClickListener(int position) {
						ArrayList<Img> imgs = new ArrayList<Img>();
						for (int i = 0; i < replyImgs.size(); i++) {
							ImgItemsBean item = replyImgs.get(i);
							String imgurl_detail = item.getImgurlbig();
							Img img = new Img(imgurl_detail);
							imgs.add(img);
						}

						Intent intent = new Intent();
						intent.setClass(mContext, ShowLargeImageActivity.class);
						intent.putExtra("imgs", imgs);
						intent.putExtra("position", position + 1);
						mContext.startActivity(intent);
					}

					@Override
					public void onDeleteClickListener(int position, ImgItemsBean imageItem) {
					}
				});
			} else {
				imageAdapter.notifyDataSetChanged();
			}
		}
	}
}
